package com.semidata.trp.algorithm.config;

import java.io.File;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Handler;
import com.semidata.rtd.core.config.Property;
import com.semidata.rtd.core.config.UrlHandler;

public class Config extends com.semidata.rtd.core.config.Config {

    private static String DEFAULT_CONFIG_FILE = "conf/algorithm.conf";
    private static Config config = null;

    public static void init(String configFile) {
        if (configFile == null)
            configFile = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath();

        config = new Config(configFile);
    }

    public static class DefaultLogDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "logs/algorithm");
            return file.getAbsolutePath();
        }

    }

    public static Config getConfig() {
        return config;
    }

    private Config(String configFile) {
        initConfig(configFile);
    }

    @Property("http.port")
    @DefaultValue("6400")
    private int httpPort;

    @Property(value = "http.threads")
    @DefaultValue("16")
    private int httpThreads;

    @Property(value = "log.root")
    @DefaultValueClass(DefaultLogDir.class)
    private String logRoot;

    @Property("log.debug.enabled")
    @DefaultValue("false")
    private boolean logDebugEnabled;

    @Property("url.algorithm")
    @DefaultValue("/algorithm")
    @Handler(UrlHandler.class)
    private String algorithmUrl;
    
    @Property("task.threads")
    @DefaultValue("16")
    private int taskThreads;
    
    @Property("run.timeout")
    @DefaultValue("10")
    private int runTimeout;
    
    @Property("timeout.compile")
    @DefaultValue("4000")
    private int compileTimeout;
    
    @Property("timeout.request")
    @DefaultValue("10")
    private int requestTimeout;
    
    public int getCompileTimeout() {
		return compileTimeout;
	}

	public int getRequestTimeout() {
		return requestTimeout;
	}

	public int getRunTimeout() {
    	return runTimeout;
    }
    
    public int getTaskThreads() {
    	return taskThreads;
    }
    public int getHttpPort() {
        return httpPort;
    }

    public int getHttpThreads() {
        return httpThreads;
    }

    public String getLogRoot() {
        return logRoot;
    }

    public boolean isLogDebugEnabled() {
        return logDebugEnabled;
    }
    
    public String getAlgorithmUrl() {
    	return this.algorithmUrl;
    }


    @Override
    public String toString() {
        return "Config [httpPort=" + httpPort + ", httpThreads=" + httpThreads + ", logRoot=" + logRoot + ", logDebugEnabled=" + logDebugEnabled;
                
    }

    public static void main(String[] args) {
        init(null);
        System.out.println(getConfig().toString());
    }

}
